package com.xbongbong.paas.elasticsearch.model.impl;

import com.xbongbong.paas.elasticsearch.model.UserTeamEsModel;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.saas.constant.StringConstant;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @author 吴峰
 * @date 2022/11/22 19:28
 */
@Service("userTeamEsModel")
public class UserTeamEsModelImpl implements UserTeamEsModel {

    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Override
    public <T> List<T> getUserTeamList(BoolQueryBuilder boolQueryBuilder, String corpid, Integer del, IndexTypeEnum indexTypeEnum, Class<T> tClass) throws XbbException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, indexTypeEnum.getType()));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL, del));
        sourceBuilder.query(boolQueryBuilder);
        // 限定索引
        SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
        searchRequest.source(sourceBuilder);
        XbbAggregatedPage<T> aggregations = xbbElasticsearchRestTemplate.queryForPages(null, searchRequest, tClass );
        return aggregations.getContent();
    }
}
